﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _12._1
{

    public class Vector
    {
        public double? R = null;
        public double? TheTa = null;
        public double? ThetaRadians
        {
            get { return (TheTa * Math.PI / 180.0); }
        }
        public Vector(double? r, double? theTa)
        {
            if(r<0)
            {
                r = -r;
                theTa += 180;
            }
            theTa = theTa % 360;
            this.R = r;
            this.TheTa = theTa;

        }

        public static Vector operator +(Vector a, Vector b)
        {
            try
            {
                double newX = (a.R.Value * Math.Sin(a.ThetaRadians.Value)) + b.R.Value * Math.Sin(b.ThetaRadians.Value);
                double newY = (a.R.Value * Math.Cos(a.ThetaRadians.Value)) + b.R.Value * Math.Cos(b.ThetaRadians.Value);
                double newR = Math.Sqrt(newX * newX + newY * newY);
                double newTheta = Math.Atan2(newX, newY) * 180 / Math.PI;
                return new Vector(newR, newTheta);

            }
            catch
            {
                return new Vector(null, null);
            }
        }

        public static Vector operator -(Vector a)=>new Vector(-a.R,a.TheTa);

        public static Vector operator -(Vector a, Vector b) => a + (-b);

        public override string ToString()
        {
            string rString = R.HasValue ? R.ToString() : "null";
            string thetaString=TheTa.HasValue? TheTa.ToString() : "null";

            return string.Format($"({rString},{thetaString})");
        }

    }
    internal class Program
    {
        static Vector GetVector(string name)
        {
            Console.WriteLine($"输入{name}名字:");
            double? r = GetNullableDouble();
            Console.WriteLine($"输入{name}名字:");
            double? theta = GetNullableDouble();

            return new Vector(r, theta);
        }

        private static double? GetNullableDouble()
        {
            double? result;
            string userInput=Console.ReadLine();
            try
            {
                result = double.Parse(userInput);
            }
            catch (Exception)
            {

                result = null;
            }
            return result;
        }

        static void Main(string[] args)
        {
            Vector v1 = GetVector("v1");
            Vector v2 = GetVector("v2");

            Console.WriteLine($"{v1}+{v2}={v1+v2}");
            Console.WriteLine($"{v1}-{v2}={v1 - v2}");

            Console.ReadLine();
        }
    }
}
